package binarySearch;

/**
 * @author understand
 * @description:时间复杂度为 O(log n)
 * @create 2025/5/18 21:40
 */
public class SearchRange {
    public static void main(String[] args) {
        int[] nums = {5,7,7,8,8,10};
        System.out.println(searchRange(nums, 8));
    }
    public static int[] searchRange(int[] nums, int target) {
        int left = 0, right = nums.length-1;
        while (left <= right){
            int mid = (left + right) / 2;
            if (nums[mid] == target)
                break;
            if (nums[mid] < target)
                left = mid + 1;
            else
                right = mid - 1;
        }
        if (left > right)
            return new int[]{-1,-1};
        int[] res = new int[2];
        for (int i = left; i <= right; i++) {
            if (nums[i] == target) {
                res[0] = i;
                break;
            }
        }
        for (int i = right; i >= left; i--) {
            if (nums[i] == target) {
                res[1] = i;
                break;
            }
        }
        return res;
    }
}
